package mysql

import (
    "strings"
    "time"

    "go.uber.org/zap"
)

type MySqlDB struct {
    dsn             string
    maxIdleConns    int
    maxOpenConns    int
    connMaxLifeTime time.Duration
    logger          *zap.Logger

    is_dev   bool
    dur_slow time.Duration
}

func New() *MySqlDB {
    x := &MySqlDB{}
    x.maxIdleConns = 10
    x.maxOpenConns = 200
    x.connMaxLifeTime = time.Hour * 6
    x.dur_slow = time.Millisecond * 500
    return x
}

func (s *MySqlDB) MaxIdleConns(n int) *MySqlDB {
    s.maxIdleConns = n
    return s
}

func (s *MySqlDB) MaxOpenConns(n int) *MySqlDB {
    s.maxOpenConns = n
    return s
}

func (s *MySqlDB) MaxConnLifeTime(d time.Duration) *MySqlDB {
    s.connMaxLifeTime = d
    return s
}

func (s *MySqlDB) WithLogger(l *zap.Logger, dev ...bool) *MySqlDB {
    s.logger = l
    if len(dev) > 0 {
        s.is_dev = dev[0]
    }
    return s
}

func (s *MySqlDB) WithSlowSQL(d time.Duration) *MySqlDB {
    s.dur_slow = d
    return s
}








func (s *MySqlDB) DSN(dsn string) *MySqlDB {
    s.dsn = dsn
    return s
}

func DSNDesensitization(dsn string) string {
    lst := strings.Split(dsn, "@")
    if len(lst) != 2 {
        return "tcp(unknown)/database"
    }
    return lst[1]
}
